const express = require('express');
const router = express.Router();
const SettingModel = require('../../model/settingModel');
const {multer} = require('../../model/tools');

router.get('/', async (req, res)=>{
  let result = await SettingModel.find({});
  res.render('admin/setting/index.html', {
    list: result[0]
  })
});

// 获取多张图片
let cpUpload = multer().fields([
  {
    name: 'site_logo',
    maxCount: 1
  },{
    name: 'no_picture',
    maxCount: 1
  }
]);
console.log(cpUpload);
router.post('/doEdit', cpUpload, async (req, res)=>{
  let json = {};
  if(req.files.site_logo){
    let site_logo = req.files.site_logo[0].path.substr(7);
    json = Object.assign(json, {site_logo:site_logo})
  }
  if(req.files.no_picture){
    let no_picture = req.files.no_picture[0].path.substr(7);
    json = Object.assign(json, {no_picture:no_picture})
  }
  await SettingModel.updateMany({}, Object.assign(req.body, json));
  res.render('admin/public/success.html', {
    message: '修改数据成功',
    redirectUrl: `/${req.app.locals.adminPath}/setting`
  });
})

module.exports = router;